package com.example.points.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.points.common.Result;
import com.example.points.domain.PointsOperationLog;
import com.example.points.domain.UserPoints;
import com.example.points.service.PointsOperationLogService;
import com.example.points.service.UserPointsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/points")
@RequiredArgsConstructor
public class UserPointsController {
    private final UserPointsService userPointsService;
    private final PointsOperationLogService operationLogService;

    @GetMapping("/{userId}")
    public Result<UserPoints> getUserPoints(@PathVariable Long userId) {
        return Result.success(userPointsService.getUserPoints(userId));
    }

    @PostMapping("/recharge/{userId}")
    public Result<Boolean> rechargePoints(@PathVariable Long userId,
                                        @RequestParam Long points,
                                        @RequestParam String description) {
        return Result.success(userPointsService.rechargePoints(userId, points, description));
    }

    @PostMapping("/consume/{userId}")
    public Result<Boolean> consumePoints(@PathVariable Long userId,
                                       @RequestParam Long points,
                                       @RequestParam String description) {
        return Result.success(userPointsService.consumePoints(userId, points, description));
    }

    @PostMapping("/batch/recharge")
    public Result<Boolean> batchRechargePoints(@RequestParam List<Long> userIds,
                                             @RequestParam Long points,
                                             @RequestParam String description) {
        return Result.success(userPointsService.batchRechargePoints(userIds, points, description));
    }

    @PostMapping("/batch/consume")
    public Result<Boolean> batchConsumePoints(@RequestParam List<Long> userIds,
                                            @RequestParam Long points,
                                            @RequestParam String description) {
        return Result.success(userPointsService.batchConsumePoints(userIds, points, description));
    }

    @GetMapping("/logs")
    public Result<IPage<PointsOperationLog>> getOperationLogs(@RequestParam(required = false) Long userId,
                                                             @RequestParam(required = false) Integer operationType,
                                                             @RequestParam(defaultValue = "1") Integer pageNum,
                                                             @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(operationLogService.getOperationLogs(userId, operationType, pageNum, pageSize));
    }
} 